package com.matrix.draft.sockettest.day8;

import com.matrix.draft.sockettest.day8.pojo.ThesisProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.stream.IntStream;

public class ProtoClientHandler extends SimpleChannelInboundHandler<String> {

    // 向服务器发送一个Proto生成的Java对象
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        ThesisProto.Thesis.Builder thesis = ThesisProto.Thesis.newBuilder()
                .setId(1)
                .setTitle("飞行汽车论文")
                .setContent("这是关于人类高质量男性的这个求偶人类高质量女性的一个论文...");
        ctx.writeAndFlush(thesis);
        // 发送5次消息
        IntStream.range(0, 5).forEach(i -> ctx.executor().execute(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ctx.writeAndFlush(thesis);
        }));
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        System.out.println(msg);
    }
}
